
package com.idealighter.game.games.tongbiniuniu.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.dictionary.dic.TongbiniuniuRoomDic;
import com.idealighter.game.dictionary.domain.TongbiniuniuRoomDomain;
import com.idealighter.game.games.common.AbstractTable;
import com.idealighter.game.games.tongbiniuniu.struct.TongBiNiuNiuRoom;
import com.idealighter.game.games.tongbiniuniu.struct.TongBiNiuNiuSeat;
import com.idealighter.game.games.tongbiniuniu.struct.TongBiNiuNiuTable;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 通比牛牛数据管理 .
 */
@Singleton
public class TongBiNiuNiuDataMgr {

  private static final Logger LOG = LoggerFactory.getLogger(TongBiNiuNiuDataMgr.class);

  private final TongbiniuniuRoomDic roomDic;
  private final Map<Integer, TongBiNiuNiuRoom> rooms;

  // 玩家的座位
  private final Map<Long, TongBiNiuNiuSeat> playerSeats = new HashMap<>();

  @Inject
  public TongBiNiuNiuDataMgr(TongbiniuniuRoomDic roomDic) {
    this.roomDic = roomDic;
    this.rooms = createRooms();
  }

  /**
   * 创建房间 .
   * 
   * @return
   */
  private Map<Integer, TongBiNiuNiuRoom> createRooms() {
    Map<Integer, TongBiNiuNiuRoom> rooms = new HashMap<>();
    for (TongbiniuniuRoomDomain roomDom : roomDic.list()) {
      rooms.put(roomDom.getId(), new TongBiNiuNiuRoom(roomDom));
      LOG.info("[通比牛牛]创建房间[{}][{}]", roomDom.getId(), roomDom.getName());
    }

    return rooms;
  }

  /**
   * 获取座位 .
   * 
   * @param playerId 玩家id.
   * @return
   */
  public TongBiNiuNiuSeat getPlayerSeat(long playerId) {

    return playerSeats.get(playerId);
  }

  /**
   * 更新玩家座位 .
   * 
   * @param playerId 玩家id.
   * @param seat 座位信息.
   */
  public void updatePlayerSeats(long playerId, TongBiNiuNiuSeat seat) {
    playerSeats.put(playerId, seat);
  }

  /**
   * 移除玩家seat .
   * 
   * @param playerId 玩家id.
   */
  public void removePlayerSeat(long playerId) {
    playerSeats.remove(playerId);
  }

  /**
   * 获取房间 .
   * 
   * @param roomId .
   * @return
   */
  public TongBiNiuNiuRoom getRoom(int roomId) {

    return rooms.get(roomId);
  }

  /**
   * 获取所有房间集合 .
   * 
   * @return
   */
  public Collection<TongBiNiuNiuRoom> rooms() {

    return rooms.values();
  }

  /**
   * 获取牌桌 .
   * 
   * @param tableId .
   * @return
   */
  public TongBiNiuNiuTable getTable(int tableId) {
    TongBiNiuNiuRoom room = getRoom(roomId(tableId));

    if (room == null) {
      return null;
    } else {
      return room.tables.get(tableId);
    }
  }

  /**
   * 根据桌子id计算房间id .
   * 
   * @param tableId .
   * @return
   */
  public int roomId(int tableId) {

    return tableId / AbstractTable.MAX_TABLE;
  }
}
